package com.meishij.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 阿飞 on 2016/10/14.
 */

public class FindWenZhangViewPagerAdapter extends FragmentPagerAdapter {

    List<Fragment> fragments;
    List<String> titles;

    public FindWenZhangViewPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
        super(fm);
        this.fragments = fragments;
        if (fragments == null) {
            this.fragments = new ArrayList<>();
        }
        this.titles = titles;
        if (titles == null) {
            this.titles = new ArrayList<>();
        }
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    public void add(Fragment fragment, String title) {
        this.fragments.add(fragment);
        this.titles.add(title);
        notifyDataSetChanged();
    }

    public void add(List<Fragment> fragments, List<String> titles) {
        this.fragments.addAll(fragments);
        this.titles.addAll(titles);
        notifyDataSetChanged();
    }

    public void clear() {
        this.fragments.clear();
        this.titles.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        int titlesCount = (titles == null ? 0 : titles.size());
        int fragmentsCount = (fragments == null ? 0 : fragments.size());
        int count = Math.min(titlesCount, fragmentsCount);
        return count;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}
